//
// Created by User on 24-10-7.
//

#ifndef ENCODER_ERROR_HPP
#define ENCODER_ERROR_HPP

#include <stdexcept>

extern "C" {
#include <libavutil/error.h>
}

namespace error
{
    class encoder_error
        : public std::runtime_error
    {
    public:
        enum class type
        {
            ENCODER_NOT_FOUND,
            CODEC_CTX_ALLOC_ERROR,
            CODEC_CTX_SET_ERROR,
            CODEC_OPEN_ERROR,
            PACKET_ALLOC_ERROR,
            CHANNEL_LAYOUT_ERROR,
            COPY_STREAM_FORBIDDEN,
            PARAMETERS_ALLOC_ERROR,
            PARAMETERS_SET_ERROR,
            SEND_FRAME_ERROR,
            RECEIVE_PACKET_ERROR,
            UNKNOWN_ERROR
        };

        encoder_error(type _type, int error_code)
            : runtime_error(""),
              m_type(_type), m_error_code(error_code)
        {
            char buffer[64]{};
            av_make_error_string(buffer, sizeof(buffer), error_code);
            this->m_msg = buffer;
        }

        encoder_error(type _type, std::string msg, int error_code = -1)
            : runtime_error(""), m_type(_type), m_error_code(error_code), m_msg(std::move(msg))
        {
        }


        [[nodiscard]] const char* what() const override
        {
            return this->m_msg.c_str();
        }

        type get_type() const
        {
            return this->m_type;
        }

        int get_error_code() const
        {
            return this->m_error_code;
        }

    private:
        type m_type{};
        int m_error_code{};
        std::string m_msg{};
    };
}

#endif //ENCODER_ERROR_HPP
